package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> selectPage(@PathVariable Integer pageNum,
                                                           @PathVariable Integer pageSize,
                                                           @RequestBody RoleDto roleDto){
        PageResponse<RoleVo> result =roleService.selectPage(pageNum,pageSize,roleDto);
        return ResponseResult.success(result);
    }

    @PutMapping
    public ResponseResult addRole(@RequestBody RoleDto roleDto){
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据id查询选中的资源")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable Long roleId){
        List<String> resourceVos = roleService.findCheckedResources(roleId);
        return ResponseResult.success(resourceVos);
    }

    @DeleteMapping("/{roleId}")
    @ApiOperation("根据id删除选中的资源")
    public ResponseResult deleteRole(@PathVariable String roleIds){
        roleService.deleteRole(roleIds);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("更新角色")
    public ResponseResult updateRole(@RequestBody RoleDto roleDto){
        Boolean result =roleService.updateRole(roleDto);
        return ResponseResult.success(result);
    }

    @PostMapping("/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult<List<RoleVo>> initRoles(){
        List<RoleVo> roleVos = roleService.getAllRoles();
        return ResponseResult.success(roleVos);
    }
}
